package pb

import (
	"packet"
)

// RoleLoginOk 登录成功
type RoleLoginOk struct {
	Uid                      uint32 //玩家ID
	Uname                    string //玩家昵称
	GoodsItems               []*GoodsItem //物品列表
	Video                    []byte //语音
}

func (this *RoleLoginOk) Encode() []byte {
	pack := this.encode()
	return pack.Encode(uint16(1020))
}

func (this *RoleLoginOk) EncodeMsg() []byte {
	pack := this.encode()
	return pack.ReadBytes()
}

func RoleLoginOkDecode(pack *packet.Packet) *RoleLoginOk {
	roleLoginOk := &RoleLoginOk{}

	roleLoginOk.Uid = pack.ReadUint32()
	roleLoginOk.Uname = pack.ReadString()
	GoodsItemsCount := pack.ReadUint16()
	for ;GoodsItemsCount > 0; GoodsItemsCount-- {
		roleLoginOk.GoodsItems = append(roleLoginOk.GoodsItems, GoodsItemDecode(pack))
	}
	VideoFlag := pack.ReadUint8()
	if VideoFlag == 1 {
		roleLoginOk.Video = pack.ReadBin()
	}

	return roleLoginOk
}

func (this *RoleLoginOk) encode() *packet.Packet {
	pack := packet.NewWriteBuff(64)

	pack.WriteUint32(this.Uid)
	pack.WriteString(this.Uname)
	GoodsItemsCount := uint16(len(this.GoodsItems))
	pack.WriteUint16(GoodsItemsCount)
	for i := uint16(0); i < GoodsItemsCount; i++ {
		pack.WriteBytes(this.GoodsItems[i].EncodeMsg())
	}
	if this.Video != nil {
		pack.WriteUint8(1)
		pack.WriteBin(this.Video)
	} else {
		pack.WriteUint8(0)
	}

	return pack
}
